package com.brk.socket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brk.socket.common.ResultCodeEnum;
import com.brk.socket.entity.LocationDO;
import com.brk.socket.entity.User;
import com.brk.socket.entity.UserDTO;
import com.brk.socket.exception.MyException;
import com.brk.socket.mapper.LocationMapper;
import com.brk.socket.mapper.UserMapper;
import com.brk.socket.service.UserService;
import com.brk.socket.util.JwtUtil;
import com.brk.socket.util.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @author brk
 * @date 2023/7/13 19:07
 * @description
 */
@Service
public class UserServiceImpl extends ServiceImpl<LocationMapper, LocationDO> implements UserService {

    @Autowired
    private LocationMapper locationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void importLocations(String polyX, String polyY) {

        LocationDO locationDO = new LocationDO(polyX, polyY);
        locationMapper.Truncate();
        locationMapper.insert(locationDO);

    }

    @Override
    public void deleteLocation() {
        locationMapper.Truncate();
    }

    @Override
    public LocationDO getRecordLocation() {
        LocationDO locationDO = locationMapper.selectOne(new QueryWrapper<>());
        return locationDO;
    }

    private User getUserByEmail(String email) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("email", email));
    }

    @Override
    public int register(User newUser){
        User user = getUserByEmail(newUser.getEmail());

        if(user != null) {
            throw new MyException(ResultCodeEnum.USER_EXIT);
        }

        return userMapper.insert(newUser);

    }


    @Override
    public String sendEmail(String email) throws Exception {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "SMTP");
        properties.setProperty("mail.smtp.host", "smtp.163.com");
        properties.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("notch_brown", "NVRZUZOWDBLZGGMV");
            }
        };
        Session session = Session.getInstance(properties, auth);

        Message message = new MimeMessage(session);
        //设置邮件的发送人
        message.setFrom(new InternetAddress("notch_brown@163.com"));
        //设置接收人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        /*获取验证码*/
        String verifyCode = VerificationCode.generateVerifyCode();

        //设置邮件标题
        message.setSubject("验证码");
        message.setContent(verifyCode, "text/html;charset=UTF-8");

        Transport.send(message);
        /*若发送成功则自动返回true*/
        return verifyCode;
    }

    @Override
    public UserDTO login(String email, String password) {
        User user = getUserByEmail(email);
        UserDTO userDTO = null;
        if(user.getPassword().equals(password)) {
            userDTO = new UserDTO();
            userDTO.setUser(user);
            userDTO.setToken(JwtUtil.sign(user, 86400000));
        }

        return userDTO;
    }


    @Override
    public int changPassword(User user) {

        return userMapper.update(user, new QueryWrapper<User>().eq("email", user.getEmail()));
    }
}
